package com.emm.yixun.openapi.hander;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.app.EditPwdReqDto;
import com.emm.yixun.common.app.EditPwdResDto;
import com.emm.yixun.openapi.dto.base.BaseRequestDTO;
import com.emm.yixun.openapi.dto.base.BaseResponseDTO;
import com.emm.yixun.openapi.vo.EditPwdReqVo;
import com.emm.yixun.system.service.app.UserServiceAppFacade;

/**
 * 修改密码接口
 * @author Administrator
 *
 */
@Service("editPwdHander")
public class EditPwdHander extends com.emm.yixun.openapi.hander.base.CommonAbstractHander{
	
	@Autowired
	private UserServiceAppFacade userServiceAppFacade;
	
	@Autowired
	private CacheManager cacheManager;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		EditPwdReqVo req = (EditPwdReqVo)reqDto;
		EditPwdReqDto reqDto1 = req.getParams();
		BaseResponseDTO<EditPwdResDto> res = new BaseResponseDTO<EditPwdResDto>();
		res.setResData(userServiceAppFacade.editPwd(reqDto1));
		//密码修改成功后，清除该用户缓存token
		if("1".equals(res.getResData().getResult())){
			Cache  cache = cacheManager.getCache("activeSessionsCache");
			String userId_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userId";//获取userId缓存key
			String token_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userId"+reqDto1.getToken();//获取token缓存key
			String userCode_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userCode";//获取userCode缓存key
			Element element_userId = cache.get(userId_key);
			Element element_token = cache.get(token_key);
			Element element_userCode = cache.get(userCode_key);
			cache.removeElement(element_userId);
			cache.removeElement(element_token);
			cache.removeElement(element_userCode);
		}
		return res;
	}
	
}
